package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipRepairRepository;
import cc.comac.mom.bean.EquipRepairRepositoryRepairStep;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipRepairRepositoryDao;
import cc.comac.mom.dao.entity.EquipRepairRepositoryRepairStepDao;
import cc.comac.mom.datahandlers.base.DataHandler;
import cc.comac.mom.entity.EquipRepairRepositoryEntity;
import cc.comac.mom.entity.EquipRepairRepositoryRepairStepEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.ResultBean;

@Component
@Transactional
public class EquipRepairRepositoryRepairStepDataHandler 
			extends ServiceImpl<EquipRepairRepositoryRepairStepDao,EquipRepairRepositoryRepairStepEntity>
			implements DataHandler<EquipRepairRepositoryRepairStep>{
	
	@Autowired
	private EquipRepairRepositoryRepairStepDao EquipRepairRepositoryRepairStepDao;
	@Autowired
	private EquipRepairRepositoryDao equipRepairRepositoryDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	
	@Override
	public ResultBean save(EquipRepairRepositoryRepairStep obj) {
		ResultBean resultBean=new ResultBean(false);
		EquipRepairRepositoryRepairStepEntity entity = obj.getInnerData();
		
		long equipRepairRepositoryId = entity.getEquipRepairRepositoryId();
		String repairItem = entity.getRepairItem();
		
		QueryWrapper<EquipRepairRepositoryRepairStepEntity> queryWrapper = new QueryWrapper<EquipRepairRepositoryRepairStepEntity>();
		queryWrapper.eq("equip_repair_repository_id", equipRepairRepositoryId);
		queryWrapper.eq("repair_item", repairItem);
		queryWrapper.eq("row_status", 1);
		List<EquipRepairRepositoryRepairStepEntity> existsEntity = EquipRepairRepositoryRepairStepDao.selectList(queryWrapper);
		
		if(existsEntity!=null && existsEntity.size()>0) {
			resultBean.setErrorMessge("equip.repair.repository.exists.repairDoc");
			resultBean.setMsgParams(new String[] {repairItem});
			return resultBean;
		}
		baseEntityDataUtil.setBaseEntityData(entity);
		boolean save = this.save(entity);
		if(!save) {
			resultBean.setErrorMessge("equip.repair.repository.saveError");
			resultBean.setMsgParams(new String[] {repairItem});
			return resultBean;
		}
		
		resultBean.setSuccess(true);
		return resultBean;
		
	}

	@Override
	public ResultBean update(EquipRepairRepositoryRepairStep obj) {
		ResultBean resultBean=new ResultBean(false);
		EquipRepairRepositoryRepairStepEntity entity = obj.getInnerData();
		
		long id = entity.getId();
		EquipRepairRepositoryRepairStepEntity entityById = super.getById(id);
		if(entityById==null) {
			resultBean.setErrorMessge("equip.repair.repository.notExists");
			resultBean.setMsgParams(new String[] {id+""});
			return resultBean;
		}
		
		long equipRepairRepositoryId = entity.getEquipRepairRepositoryId();
		String repairItem = entity.getRepairItem();
		
		QueryWrapper<EquipRepairRepositoryRepairStepEntity> queryWrapper = new QueryWrapper<EquipRepairRepositoryRepairStepEntity>();
		queryWrapper.eq("equip_repair_repository_id", equipRepairRepositoryId);
		queryWrapper.eq("repair_item", repairItem);
		queryWrapper.ne("equip_repair_repository_repair_step_id", id);
		queryWrapper.eq("row_status", 1);
		List<EquipRepairRepositoryRepairStepEntity> existsEntity = EquipRepairRepositoryRepairStepDao.selectList(queryWrapper);
		
		if(existsEntity!=null && existsEntity.size()>0) {
			resultBean.setErrorMessge("equip.repair.repository.exists.repairStep");
			resultBean.setMsgParams(new String[] {repairItem});
			return resultBean;
		}
		baseEntityDataUtil.updateBaseEntityData(entity);
		boolean save = this.updateById(entity);
		if(!save) {
			resultBean.setErrorMessge("equip.repair.repository.saveError");
			resultBean.setMsgParams(new String[] {repairItem});
			return resultBean;
		}
		
		resultBean.setSuccess(true);
		return resultBean;
		
	}

	@Override
	public ResultBean delete(EquipRepairRepositoryRepairStep obj) {
		ResultBean resultBean=new ResultBean(false);
		EquipRepairRepositoryRepairStepEntity entity = obj.getInnerData();
		
		long id = entity.getId();
		EquipRepairRepositoryRepairStepEntity existsEntity = EquipRepairRepositoryRepairStepDao.selectById(id);
		if(existsEntity==null) {
			resultBean.setErrorMessge("equip.repair.repository.notExists");
			resultBean.setMsgParams(new String[] {id+""});
			return resultBean;
		}
		baseEntityDataUtil.deleteBaseEntityData(existsEntity);
		boolean save = this.updateById(entity);
		if(!save) {
			resultBean.setErrorMessge("equip.repair.repository.saveError");
			resultBean.setMsgParams(new String[] {entity.getRepairItem()});
			return resultBean;
		}
		
		resultBean.setSuccess(true);
		return resultBean;
	}

	@Override
	public ResultBean save(List<EquipRepairRepositoryRepairStep> objs) {
		ResultBean resultBean=new ResultBean(false);
		List<EquipRepairRepositoryRepairStepEntity> entityList = new ArrayList<>();
				
		for(EquipRepairRepositoryRepairStep obj:objs) {
			EquipRepairRepositoryRepairStepEntity entity = obj.getInnerData();
			
			long equipRepairRepositoryId = entity.getEquipRepairRepositoryId();
			String repairItem = entity.getRepairItem();
			
			QueryWrapper<EquipRepairRepositoryRepairStepEntity> queryWrapper = new QueryWrapper<EquipRepairRepositoryRepairStepEntity>();
			queryWrapper.eq("equip_repair_repository_id", equipRepairRepositoryId);
			queryWrapper.eq("repair_item", repairItem);
			queryWrapper.eq("row_status", 1);
			List<EquipRepairRepositoryRepairStepEntity> existsEntity = EquipRepairRepositoryRepairStepDao.selectList(queryWrapper);
			
			if(existsEntity!=null && existsEntity.size()>0) {
				resultBean.setErrorMessge("equip.repair.repository.exists.repairDoc");
				resultBean.setMsgParams(new String[] {repairItem});
				return resultBean;
			}
			baseEntityDataUtil.setBaseEntityData(entity);
			entityList.add(entity);
		}
		
		QueryWrapper<EquipRepairRepositoryRepairStepEntity> queryWrapper1 = new QueryWrapper<EquipRepairRepositoryRepairStepEntity>();
		queryWrapper1.eq("equip_repair_repository_id", objs.get(0).getInnerData().getEquipRepairRepositoryId());
		queryWrapper1.eq("row_status", 1);
		queryWrapper1.orderByDesc("repair_sequence");
		List<EquipRepairRepositoryRepairStepEntity> listEntity = EquipRepairRepositoryRepairStepDao.selectList(queryWrapper1);
		int i=0;
		if(listEntity != null && listEntity.size()>0) {
			i = listEntity.get(0).getRepairSequence()+1;
		}
    	for(int b= i;b<entityList.size()+i;b++) {
    		entityList.get(b-i).setRepairSequence(b);
    	}
		
		boolean save = this.saveBatch(entityList);
		if(!save) {
			resultBean.setErrorMessge("equip.repair.repository.saveError");
			return resultBean;
		}
		
		resultBean.setSuccess(true);
		return resultBean;
	}

	@Override
	public ResultBean update(List<EquipRepairRepositoryRepairStep> objs) {
		ResultBean resultBean=new ResultBean(false);
		List<EquipRepairRepositoryRepairStepEntity> entityList = new ArrayList<>();
		for(EquipRepairRepositoryRepairStep obj:objs) {
			EquipRepairRepositoryRepairStepEntity entity = obj.getInnerData();
			
			long id = entity.getId();
			EquipRepairRepositoryRepairStepEntity entityById = super.getById(id);
			if(entityById==null) {
				resultBean.setErrorMessge("equip.repair.repository.notExists");
				resultBean.setMsgParams(new String[] {id+""});
				return resultBean;
			}
			
			long equipRepairRepositoryId = entity.getEquipRepairRepositoryId();
			String repairItem = entity.getRepairItem();
			
			QueryWrapper<EquipRepairRepositoryRepairStepEntity> queryWrapper = new QueryWrapper<EquipRepairRepositoryRepairStepEntity>();
			queryWrapper.eq("equip_repair_repository_id", equipRepairRepositoryId);
			queryWrapper.eq("repair_item", repairItem);
			queryWrapper.ne("equip_repair_repository_repair_step_id", id);
			queryWrapper.eq("row_status", 1);
			List<EquipRepairRepositoryRepairStepEntity> existsEntity = EquipRepairRepositoryRepairStepDao.selectList(queryWrapper);
			
			if(existsEntity!=null && existsEntity.size()>0) {
				resultBean.setErrorMessge("equip.repair.repository.exists.repairStep");
				resultBean.setMsgParams(new String[] {repairItem});
				return resultBean;
			}
			baseEntityDataUtil.updateBaseEntityData(entity);
			entityList.add(entity);
		}
		
		boolean save = this.updateBatchById(entityList);
		if(!save) {
			resultBean.setErrorMessge("equip.repair.repository.saveError");
			return resultBean;
		}
		resultBean.setSuccess(true);
		return resultBean;
	}

	@Override
	public ResultBean delete(List<EquipRepairRepositoryRepairStep> objs) {
		ResultBean resultBean=new ResultBean(false);
		List<EquipRepairRepositoryRepairStepEntity> entityList = new ArrayList<>();
		for(EquipRepairRepositoryRepairStep obj:objs) {
			EquipRepairRepositoryRepairStepEntity entity = obj.getInnerData();
			long id = entity.getId();
			EquipRepairRepositoryRepairStepEntity existsEntity = EquipRepairRepositoryRepairStepDao.selectById(id);
			if(existsEntity==null) {
				resultBean.setErrorMessge("equip.repair.repository.notExists");
				resultBean.setMsgParams(new String[] {entity.getRepairItem()});
				return resultBean;
			}
			baseEntityDataUtil.deleteBaseEntityData(existsEntity);
			
			entityList.add(entity);
		}
		
		boolean save = this.updateBatchById(entityList);
		if(!save) {
			resultBean.setErrorMessge("equip.repair.repository.saveError");
			return resultBean;
		}
		
		resultBean.setSuccess(true);
		return resultBean;
	}

	@Override
	public EquipRepairRepositoryRepairStep getById(long id) {
		EquipRepairRepositoryRepairStepEntity entity = super.getById(id);
		if(entity!=null) {
			EquipRepairRepositoryRepairStep bean = new EquipRepairRepositoryRepairStep();
			bean.setInnerData(entity);
			return bean;
		}
		return null;
	}
	
	
	public ResultBean moveUp(EquipRepairRepositoryRepairStep obj) {
		ResultBean resultBean = new ResultBean(true);
		//校验数据
		if(obj == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;
		}
		EquipRepairRepositoryRepairStepEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;	
		}
		EquipRepairRepositoryRepairStepEntity selectById = EquipRepairRepositoryRepairStepDao.selectById(innerData.getId());
		if(selectById == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("上移数据为空");
			return resultBean;	
		}
    	QueryWrapper<EquipRepairRepositoryRepairStepEntity> queryWrapper = new QueryWrapper<EquipRepairRepositoryRepairStepEntity>();
    	queryWrapper.eq("row_status",1);
    	queryWrapper.eq("equip_repair_repository_id",selectById.getEquipRepairRepositoryId());
    	queryWrapper.lt("repair_sequence", selectById.getRepairSequence());
    	queryWrapper.orderByDesc("repair_sequence");
    	Page<EquipRepairRepositoryRepairStepEntity> page = new Page<>(0,1, false);
    	IPage<EquipRepairRepositoryRepairStepEntity> iPage = EquipRepairRepositoryRepairStepDao.selectPage(page, queryWrapper);
    	List<EquipRepairRepositoryRepairStepEntity> list = iPage.getRecords();
    	if(list == null || list.size()<1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("已经是第一条数据，无法上移");
			return resultBean;	
    	}
    	EquipRepairRepositoryRepairStepEntity equipRepairRepositoryRepairStepEntity = list.get(0);
    	
    	int temp = selectById.getRepairSequence();
    	selectById.setRepairSequence(equipRepairRepositoryRepairStepEntity.getRepairSequence());
    	equipRepairRepositoryRepairStepEntity.setRepairSequence(temp);
    	baseEntityDataUtil.updateBaseEntityData(selectById);
    	baseEntityDataUtil.updateBaseEntityData(equipRepairRepositoryRepairStepEntity);
    	
    	ArrayList<EquipRepairRepositoryRepairStepEntity> arrayList = new ArrayList<EquipRepairRepositoryRepairStepEntity>();
    	arrayList.add(equipRepairRepositoryRepairStepEntity);
    	arrayList.add(selectById);
    	boolean batchById = updateBatchById(arrayList);

		if(!batchById) {
			resultBean.setSuccess(false);
			resultBean.setMessage("上移失败");
			return resultBean;
		}
		return resultBean;
	}

	
	public ResultBean moveDown(EquipRepairRepositoryRepairStep obj) {
		ResultBean resultBean = new ResultBean(true);
		//校验数据
		if(obj == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;
		}
		EquipRepairRepositoryRepairStepEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数据为空");
			return resultBean;	
		}
		EquipRepairRepositoryRepairStepEntity selectById = EquipRepairRepositoryRepairStepDao.selectById(innerData.getId());
		if(selectById == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("下移数据为空");
			return resultBean;	
		}
    	QueryWrapper<EquipRepairRepositoryRepairStepEntity> queryWrapper = new QueryWrapper<EquipRepairRepositoryRepairStepEntity>();
    	queryWrapper.eq("row_status",1);
    	queryWrapper.eq("equip_repair_repository_id",selectById.getEquipRepairRepositoryId());
    	queryWrapper.gt("repair_sequence", selectById.getRepairSequence());
    	queryWrapper.orderByAsc("repair_sequence");
    	Page<EquipRepairRepositoryRepairStepEntity> page = new Page<>(0,1, false);
    	IPage<EquipRepairRepositoryRepairStepEntity> iPage = EquipRepairRepositoryRepairStepDao.selectPage(page, queryWrapper);
    	List<EquipRepairRepositoryRepairStepEntity> list = iPage.getRecords();
    	if(list == null || list.size()<1) {
			resultBean.setSuccess(false);
			resultBean.setMessage("已经是最后一条数据，无法下移");
			return resultBean;	
    	}
    	EquipRepairRepositoryRepairStepEntity equipRepairRepositoryRepairStepEntity = list.get(0);
    	
    	int temp = selectById.getRepairSequence();
    	selectById.setRepairSequence(equipRepairRepositoryRepairStepEntity.getRepairSequence());
    	equipRepairRepositoryRepairStepEntity.setRepairSequence(temp);
    	baseEntityDataUtil.updateBaseEntityData(selectById);
    	baseEntityDataUtil.updateBaseEntityData(equipRepairRepositoryRepairStepEntity);
    	
    	ArrayList<EquipRepairRepositoryRepairStepEntity> arrayList = new ArrayList<EquipRepairRepositoryRepairStepEntity>();
    	arrayList.add(equipRepairRepositoryRepairStepEntity);
    	arrayList.add(selectById);
    	
    	boolean batchById = updateBatchById(arrayList);

		if(!batchById) {
			resultBean.setSuccess(false);
			resultBean.setMessage("下移失败");
			return resultBean;
		}
		return resultBean;
	}
	
	public ResultDev selectByCondition(EquipRepairRepository obj) {
		ResultDev resultDev = new ResultDev();
    	QueryWrapper<EquipRepairRepositoryEntity> queryWrapper = new QueryWrapper<EquipRepairRepositoryEntity>();
    	queryWrapper.eq("row_status",1);
    	queryWrapper.eq("equip_category_id",obj.getInnerData().getEquipCategoryId());
    	queryWrapper.eq("fault_cause_id",obj.getInnerData().getFaultCauseId());
    	
    	List<EquipRepairRepositoryEntity> listEquipRepairRepositoryEntity = equipRepairRepositoryDao.selectList(queryWrapper);

    	if(listEquipRepairRepositoryEntity == null || listEquipRepairRepositoryEntity.size()<1) {
    		return null;
    	}
    	EquipRepairRepositoryEntity equipRepairRepositoryEntity = listEquipRepairRepositoryEntity.get(0);
    	QueryWrapper<EquipRepairRepositoryRepairStepEntity> queryWrapper1 = new QueryWrapper<EquipRepairRepositoryRepairStepEntity>();
    	queryWrapper1.eq("row_status",1);
    	queryWrapper1.eq("equip_repair_repository_id",equipRepairRepositoryEntity.getId());
    	if(!StringUtils.isEmpty(obj.getRepairItem())){
			queryWrapper1.eq("repair_item", obj.getRepairItem());
		}
		if(!StringUtils.isEmpty(obj.getRepairContent())) {
			queryWrapper1.eq("repair_content", obj.getRepairContent());
		}
    	queryWrapper1.orderByAsc("repair_sequence");
    	
        Page<EquipRepairRepositoryRepairStepEntity> page = new Page<>(obj.getPageNum(), obj.getPageSize(), true);
        IPage<EquipRepairRepositoryRepairStepEntity> iPage = EquipRepairRepositoryRepairStepDao.selectPage(page, queryWrapper1);
        List<EquipRepairRepositoryRepairStepEntity> list = iPage.getRecords();
        
		resultDev.setEntityList(list);
		resultDev.setTotalQuantity((int)iPage.getTotal());
		resultDev.setPageCount((int)iPage.getPages());
		return resultDev;
		
	}
}
